package cn.zs.supply.web.controller;
import cn.zs.supply.common.constants.enums.PubErrCode;
import cn.zs.supply.common.core.domain.result.BaseResult;
import cn.zs.supply.common.core.domain.result.ObjectResult;
import cn.zs.supply.common.core.domain.result.PageResult;
import cn.zs.supply.common.core.domain.result.ResultBuilder;
import cn.zs.supply.common.core.exception.BusiException;
import cn.zs.supply.common.utils.BeanValidators;
import cn.zs.supply.dto.UserDto;
import cn.zs.supply.query.UserQuery;
import cn.zs.supply.service.TestService;
import cn.zs.supply.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Api("测试接口")
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
    @Autowired
    private TestService testService;

    @ApiOperation(value = "新增用户", notes = "")
    @PostMapping("/user/add")
    public BaseResult addUser(@RequestBody @Valid UserDto userDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new BusiException(PubErrCode.PARAM_ERROR, BeanValidators.buildErrMsg(result));
        }
        testService.addUser(userDto);
        return ResultBuilder.genSuccessBaseResult();
    }

    @ApiOperation(value = "用户详情", notes = "")
    @GetMapping("/user/{id}")
    public ObjectResult<UserVo> getUser(@PathVariable(name = "id") @ApiParam(name = "id", value = "用户id") Long userId) {
        UserVo userVo = testService.getUser(userId);
        return ResultBuilder.genSuccessObjectResult(userVo);
    }


    @ApiOperation(value = "用户列表", notes = "")
    @PostMapping("/user/list")
    public PageResult<UserVo> getUserList(@RequestBody @Valid UserQuery query) {
        return testService.getUserList(query);
    }


}
